package com.tan.androidtest.net;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.zhy.http.okhttp.callback.Callback;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/12/5.
 */

public abstract class HttpCallBack<T> extends Callback<T> {

    private Type type;

    public HttpCallBack() {
        type = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

    @Override
    public T parseNetworkResponse(Response response, int id) throws Exception {
        try {
            if (response.isSuccessful()) {
                String str = response.body().string();
                Log.i("==--", str);
                T httpResponse = JSON.parseObject(str, type);
                return httpResponse;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void onError(Call call, Exception e, int id) {
        Log.i("==--", "请求数据失败: " + call.request().url().url().getPath());
    }
}
